<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">

    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>预约管理<small>预约项管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>预约管理</el-breadcrumb-item>
                    <el-breadcrumb-item>预约项管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="手机号/姓名" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                        <el-button type="danger" class="butT" @click="handleCancelChecked()">批量取消预约</el-button>
                    </div>
                    <el-table size="small" :data="dataList" stripe highlight-current-row @selection-change="handleSelectionChange">

                        <el-table-column type="selection"></el-table-column>

                        <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                        <el-table-column prop="phoneNumber" label="手机号码" align="center"></el-table-column>
                        <el-table-column prop="orderType" label="预约类型" align="center"></el-table-column>
                        <el-table-column prop="orderStatus" label="预约状态" align="center"></el-table-column>
                        <el-table-column prop="setmealName" label="套餐名称" align="center"></el-table-column>
                        <el-table-column prop="orderDate" label="预约体检日期" align="center"></el-table-column>

                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleConfirm(scope.row)">确认</el-button>
                                <el-button size="mini" type="danger" @click="handleCancel(scope.row)">取消</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增预约" :visible.sync="dialogFormVisible">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="预约基本信息" name="first">
                                        <el-form ref="addFormData" :model="formData" :rules="rules" label-position="right" label-width="90px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="姓名" prop="name">
                                                        <el-input v-model="formData.name"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="身份证号" prop="idCard">
                                                        <el-input v-model="formData.idCard"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>

                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="出生日期" prop="birthDay">
                                                        <el-date-picker
                                                                v-model="formData.birthDay"
                                                                type="date"
                                                                value-format="yyyy-MM-dd"
                                                                placeholder="选择日期">
                                                        </el-date-picker>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="手机号码" prop="phoneNumber">
                                                        <el-input v-model="formData.phoneNumber"></el-input>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="性别" prop="sex">
                                                        <el-select v-model="formData.sex">
                                                            <el-option label="男" value="1"></el-option>
                                                            <el-option label="女" value="2"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="年龄" prop="age">
                                                        <el-input v-model="formData.age"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="婚姻状况" prop="maritalStatus">
                                                        <el-select v-model="formData.maritalStatus">
                                                            <el-option label="未婚" value="0"></el-option>
                                                            <el-option label="已婚" value="1"></el-option>
                                                            <el-option label="离异" value="2"></el-option>
                                                            <el-option label="丧偶" value="3"></el-option>
                                                        </el-select>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="预约日期" prop="orderDate">
                                                        <el-date-picker
                                                                v-model="formData.orderDate"
                                                                type="date"
                                                                value-format="yyyy-MM-dd"
                                                                placeholder="选择日期">
                                                        </el-date-picker>
                                                    </el-form-item>
                                                </el-col>

                                            </el-row>

                                        </el-form>
                                    </el-tab-pane>
                                    <el-tab-pane label="检查套餐信息" name="second">
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择</th>
                                                    <th>套餐编码</th>
                                                    <th>套餐名称</th>
                                                    <th>套餐说明</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="s in tableData">
                                                    <td>
                                                        <input :id="s.id" v-model="setmealIds" type="checkbox" :value="s.id">
                                                    </td>
                                                    <td><label :for="s.id">{{s.code}}</label></td>
                                                    <td><label :for="s.id">{{s.name}}</label></td>
                                                    <td><label :for="s.id">{{s.remark}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>


                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
                //日期选择器的数据
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    },
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                activeName:"first",
                dataList: [],//当前页要展示的分页列表数据
                tableData:[],
                setmealIds:[],     //新增或者编辑预约时，表示套餐的id
                formData: {},//表单数据
                multipleSelection: [],    //复选框的数据(其中为选中行对象)
                orderIds:[],
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则
                    //用户名验证非空，长度3-5
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' },
                        { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                    ],
                    //身份证号验证15位数字、18位数字、17位数字加上X/x
                    /*
                    也可将reg正则定义为常量，在验证规则加上validator属性
                    { validator: isMobileNumber, trigger: "blur" }
                    */
                    idCard: [
                        { required: true, pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
                        message: '请输入正确的身份证号码', trigger: 'blur' }
                    ],
                    //出生日期验证非空
                    birthDay: [{ required: true, message: '请选择出生日期', trigger: 'blur' }],
                    //手机号验证
                    phoneNumber: [
                        { required: true, pattern: /^1[34578]\d{9}$/,
                            message: '请输入正确的手机号码', trigger: 'blur'}
                    ],
                    //性别验证(选择项只用判断非空)
                    sex: [{ required: true, message: '性别为必填项', trigger: 'blur' }],
                    //年龄验证()
                    age: [
                        { required: true, message: '年龄为必填项', trigger: 'blur' },
                        { pattern:/(^[1][01234]\d$)|(^[123456789]\d$)|(^[123456789]$)/,
                            message: '年龄必须为小于150岁',  trigger: 'blur'
                        }
                    ],
                    //预约日期验证非空
                    orderDate: [{ required: true, message: '请选择预约时间', trigger: 'blur' }]
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                //vue创建完成后就查询所有检查项
                this.findPage();
            },
            methods: {
                //权限不足提示
                showMessage(r) {  
                    if(r == 'Error: Request failed with status code 403') {
                        //权限不足    
                        this.$message.error('无访问权限');    
                        return;
                    }else{    
                        this.$message.error('未知错误');    
                        return;
                    }
                },

                //添加
                handleAdd () {
                    //对表单进行验证
                    this.$refs['addFormData'].validate((valid)=>{
                        if(valid){
                            //此时验证成功,发送axios请求
                            axios.post("/orderSettingList/add.do?setmealIds="+this.setmealIds,this.formData)
                                .then((res)=>{
                                    //处理数据
                                    if(res.data.flag){
                                        //添加成功，提示消息，关闭对话框
                                        this.$message.success(res.data.message);
                                        this.dialogFormVisible=false;
                                        //重新查询
                                        this.findPage();
                                    }else{
                                        this.$message.error(res.data.message);
                                    }
                                })
                                .catch((error)=>{
                                    console.log(error)
                                })
                        }else{
                            return false;
                        }
                    });
                },
                //分页查询
                findPage() {
                    //发送异步请求，查询所有预约数据(条件的分页查询，需要携带当前页，每页多少条，查询条件)
                    var paras ={
                        currentPage: this.pagination.currentPage,
                        pageSize: this.pagination.pageSize,
                        queryString: this.pagination.queryString
                    };
                    axios.post("/orderSettingList/findPage.do",paras)
                        .then((res)=>{
                            this.dataList=res.data.rows;
                            this.pagination.total=res.data.total;
                        })
                        .catch((error)=>{
                            this.showMessage(error);
                        })
                },
                // 重置表单
                resetForm() {
                    this.formData={};
                    this.activeName="first";
                },
                // 弹出添加窗口
                handleCreate() {
                    //弹出新建表单的弹窗之前先将form表单的数据清空
                    this.resetForm();
                    //设置dialogFormVisible的值来弹窗
                    this.dialogFormVisible=true;
                    this.setmealIds=[];

                    //此时需要发送异步请求，查询所有的套餐信息
                    axios.get("/setmeal/findAll.do")
                        .then((res)=>{
                            this.tableData=res.data.data;
                        })
                        .catch((error)=>{
                            console.log(error);
                        })
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;
                    //切换页码是，更新当前页，发送请求
                    this.findPage();
                },

                //确认到诊
                handleConfirm(row) {
                    //直接发送异步请求，携带预约id
                    axios.post("/orderSettingList/confirm/"+row.id+".do")
                        .then((res)=>{
                            if(res.data.flag){
                                //取消成功，消息提示，重新查询
                                this.$message.success(res.data.message);
                                this.findPage();
                            }else{
                                //取消失败,消息提示
                                this.$message.error(res.data.message);
                            }
                        })
                },

                // 取消预约
                handleCancel(row) {
                    //获取当前行预约的id

                    //发送异步请求，取消此条预约(携带参数为当前预约id)
                    axios.post("/orderSettingList/cancel/"+row.id+".do")
                        .then((res)=>{
                            if(res.data.flag){
                                //取消成功，消息提示，重新查询
                                this.$message.success(res.data.message);
                                this.findPage();
                            }else{
                                //取消失败,消息提示
                                this.$message.error(res.data.message);
                            }
                        })
                },
                // 批量取消预约
                handleCancelChecked(rows) {
                    //获取当前选中预约对象的ids
                    //此时就需要从multipleSelection数组中取出每行预约对象的id
                    for(i=0; i<this.multipleSelection.length; i++){
                        var selectionElement = this.multipleSelection[i];
                        this.orderIds.push(selectionElement.id);
                    }

                    //发送异步请求，取消选中的预约(携带参数为当前选中的预约ids)
                    axios.post("/orderSettingList/cancelChecked.do?orderIds="+this.orderIds)
                        .then((res)=>{
                            if(res.data.flag){
                                //取消成功，消息提示，重新查询
                                this.$message.success(res.data.message);
                                this.findPage();
                            }else{
                                //取消失败,消息提示
                                this.$message.error(res.data.message);
                            }
                        });
                    //将存放选中预约id的集合清空
                    this.orderIds=[];
                },
                //复选框的方法
                toggleSelection(rows) {
                    if (rows) {
                        rows.forEach(row => {
                            this.$refs.multipleTable.toggleRowSelection(row);
                        });
                    } else {
                        this.$refs.multipleTable.clearSelection();
                    }
                },
                handleSelectionChange(val) {
                    this.multipleSelection = val;
                }
            }
        })
    </script>
</html>